/*++

Copyright (c) 1989 - 1999 Microsoft Corporation

Module Name:

    write.c

Abstract:

    This module implements the mini redirector call down routines pertaining
    to write of file system objects.

--*/

#include "precomp.h"
#pragma hdrstop

#include "mrxglobs.h"
#include "host_access.h"

NTSTATUS
HwFsWrite(
    IN PRX_CONTEXT RxContext)
/*++

Routine Description:

   This routine opens a file across the network.

Arguments:

    RxContext - the RDBSS context

Return Value:

    RXSTATUS - The return status for the operation

--*/

{
    NTSTATUS Status = STATUS_SUCCESS;

    PHWFS_DEVICE_EXTENSION   pDevExt = HWFS_GET_DEVICE_EXTENSION(RxContext);

    __notnull PMRX_FCB Fcb = RxContext->pFcb;
    __notnull PHWFS_FCB_EXTENSION pFcbExt = HWFS_GET_FCB_EXTENSION(Fcb);

    PHWFS_FILE_INFO pFi = &pFcbExt->FileInfo;

    PMRX_SRV_OPEN SrvOpen = RxContext->pRelevantSrvOpen;
    HWFS_SRVOPEN_EXTENSION* pSrvOpenExt = SrvOpen->Context;

    PLOWIO_CONTEXT LowIoContext  = &RxContext->LowIoContext;

    PMDL pBufferMdl = LowIoContext->ParamsFor.ReadWrite.Buffer;
    ULONG ByteCount = LowIoContext->ParamsFor.ReadWrite.ByteCount;
    RXVBO ByteOffset = LowIoContext->ParamsFor.ReadWrite.ByteOffset;

    BOOLEAN SynchronousIo = !BooleanFlagOn(RxContext->Flags, RX_CONTEXT_FLAG_ASYNC_OPERATION);

    RxContext->InformationToReturn = 0;

    HWFS_DBG_PRINT(DBG_TRACE, DBG_FWRITE,
        ("HwFsWrite ByteCount 0x%x, ByteOffset 0x%x\n", ByteCount, ByteOffset));

    Status = HostWriteFile(pDevExt, &pSrvOpenExt->HwfsFile,
        ByteOffset, pBufferMdl, ByteCount, &RxContext->InformationToReturn);

    HWFS_DBG_PRINT(DBG_TRACE, DBG_FWRITE,
        ("Write Finished. Status = 0x%x BytesDone = 0x%I64x\n",
            Status, RxContext->InformationToReturn));

    if (NT_SUCCESS(Status)) {
        UINT64 CurrPtr = ByteOffset + RxContext->InformationToReturn;
        if (CurrPtr > (UINT64)pFi->StandardInfo.EndOfFile.QuadPart) {
            HWFS_DBG_PRINT(DBG_TRACE, DBG_FWRITE, ("New file size: 0x%I64x\n", CurrPtr));
            pFi->StandardInfo.EndOfFile.QuadPart = CurrPtr;

            if (CurrPtr > (UINT64)pFi->StandardInfo.AllocationSize.QuadPart) {
                pFi->StandardInfo.AllocationSize.QuadPart = ROUND_TO_PAGES(CurrPtr);
            }
        }
    }

    return(Status);
}
